package com.example.demo.mapper;


import com.example.demo.entity.Teacher;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Optional;

// 标记这是一个 MyBatis Mapper 接口
@Mapper
public interface TeacherMapper {

    // 1. 查询所有教师
    @Select("SELECT teacher_id, teacher_name, password, created_at, updated_at FROM teachers")
    List<Teacher> findAll();

    // 2. 根据工号查询单个教师
    // 使用 Optional 包装返回结果，以处理查询不到的情况
    @Select("SELECT teacher_id, teacher_name, password, created_at, updated_at FROM teachers WHERE teacher_id = #{teacherId}")
    Optional<Teacher> findById(@Param("teacherId") String teacherId);

    // 3. 插入新教师
    // @Options(useGeneratedKeys = true, keyProperty = "id") 适用于自增主键，这里主键不是自增，不使用
    @Insert("INSERT INTO teachers (teacher_id, teacher_name, password) " +
            "VALUES (#{teacherId}, #{teacherName}, #{password})")
    int insert(Teacher teacher);

    // 4. 更新教师信息
    // 注意：updated_at 在数据库层面的 ON UPDATE CURRENT_TIMESTAMP 会自动更新
    @Update("UPDATE teachers SET teacher_name = #{teacherName}, password = #{password} " +
            "WHERE teacher_id = #{teacherId}")
    int update(Teacher teacher);

    // 5. 根据工号删除教师
    @Delete("DELETE FROM teachers WHERE teacher_id = #{teacherId}")
    int deleteById(@Param("teacherId") String teacherId);
}